﻿using AutoMapper;
using MediatR;
using S.Archives.Api.Application.Command.Wander;
using S.Archives.Domain.S.ArchivesModel;
using S.Archives.Infrstructre.InterFace;
using S.Common.Enums.Archives;

namespace S.Archives.Api.Application.CommandHandlers.Wander
{
    /// <summary>
    /// 批量动态修改流转状态
    /// </summary>
    public class WanderStateDynamicEditPLCommandHandler : IRequestHandler<WanderStateDynamicEditPLCommand, bool>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        public WanderStateDynamicEditPLCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
        }

        public async Task<bool> Handle(WanderStateDynamicEditPLCommand request, CancellationToken cancellationToken)
        {
            List<SheepFileModel> sheepFileModels = new List<SheepFileModel>();

            var sheep = sheepFileRepository.GetAll();
            foreach (var item in request.Wander)
            {
               var a= sheep.FirstOrDefault(x=>x.EarTag == item.EarTag);
                if (a != null)
                {
                    a.WanderReproductiveState =(WanderReproductiveStateEnum) item.WanderReproductiveState;
                    a.UpdateDate = DateTime.Now;
                    sheepFileModels.Add(a);
                   
                }
                else
                {
                    return false;
                }
               
            }
            return await sheepFileRepository.UpdateRangeAsync(sheepFileModels);
        }
    }
}
